<?php
namespace app\admin\service;
use think\Db;
class  Order{

    //订单列表
    function orderList(){
        $uid = input('uid');
        $where = null;
        if(!empty($uid)){
            $where['a.u_uid'] = $uid;
        }
        $o_type_id = input('o_type_id');
        if(!empty($o_type_id)){
            if($o_type_id == 1){
                $where['a.o_type_id'] = array('in','11,12,13,14');
            }else{
                $where['a.o_type_id'] = $o_type_id;
            }

        }
        $o_state = input('o_state');
        if(!empty($o_state)){
            $where['a.o_state'] = $o_state;
        }
        $stime  = input('stime');
        $otime  = input('otime');
        if(!empty($stime) || !empty($otime)){
            if(!empty($stime) && !empty($otime)){
                $where['a.o_addtime']=array(array('egt',"{$stime}%"),array('elt',"{$otime}%")) ;
            }else if(!empty($stime)){
                $where['a.o_addtime'] = array('gt',$stime);
            }else{
                $where['a.o_addtime'] = array('gt',$otime);
            }
        }
        $join = [
            ['lt_user b','a.u_uid=b.u_uid'],
        ];
        $param         = input ( 'param.' );
        $field    = 'a.o_no,a.order_id,a.o_type_id,a.o_money,a.o_state,a.o_addtime,a.o_paytime,b.u_phone,b.u_nickname';
        $paginate = 15;
        $list     = Db::name('order')->alias('a')->join($join)->where($where)->field($field)->order('a.o_addtime desc')->paginate($paginate);
        $page     = $list->render();
        $data['list'] = $list;
        $data['page'] = $page;
        $data['post'] = $param?$param:null;
        return $data;
    }

    //订单详情
    function orderFind(){
        $order_id = input('order_id');
        $join = [
            ['lt_user b','a.u_uid=b.u_uid'],
        ];
        $where['a.order_id'] = $order_id;
        $field    = 'a.*,b.u_phone,b.u_nickname';
        $data     = Db::name('order')->alias('a')->join($join)->where($where)->field($field)->find();
        return    $data;
    }

    //订单操作
    function orderHandleApi($order_id,$state){
        if(empty($order_id) || empty($state)){
            return messageInfo('101','系统参数错误!');
        }
        if($state == 1){
            $value = 3;
        }else if($state == 2){
            $value = 4;
        }else{
            $value = 2;
        }
        $date = date('Y-m-d H:i:s');
        $data_arr = array(
          'o_state'      => $value,
          'o_handletime' => $date
        );
        if($state == 3){
            $data_arr['o_paytime'] = $date;
        }
        $a     = Db::name('order')->where('order_id',$order_id)->update($data_arr);
        if(!$a){
            return messageInfo('101','操作失败!');
        }
        return messageInfo('202','操作成功!');
    }


    //订单列表
    function orderServeList(){
        $uid = input('uid');
        $where = null;
        if(!empty($uid)){
            $where['a.u_uid'] = $uid;
        }
        $co_name = input('co_name');
        if(!empty($co_name)){
            $where['a.co_name'] = array('like',"$co_name%");
        }
        $co_state = input('co_state');
        if(!empty($co_state)){
            $where['a.co_state'] = $co_state;
        }
        $stime  = input('stime');
        $otime  = input('otime');
        if(!empty($stime) || !empty($otime)){
            if(!empty($stime) && !empty($otime)){
                $where['a.co_addtime']=array(array('egt',"{$stime}%"),array('elt',"{$otime}%")) ;
            }else if(!empty($stime)){
                $where['a.co_addtime'] = array('gt',$stime);
            }else{
                $where['a.co_addtime'] = array('gt',$otime);
            }
        }
        $join = [
            ['lt_user b','a.u_uid=b.u_uid'],
        ];
        $param         = input ( 'param.' );
        $field    = 'a.counselor_order_id,a.co_no,a.co_state,a.co_name,a.co_money,a.co_age_limit,a.co_addtime,a.co_begintime,a.co_finishtime,b.u_phone,b.u_nickname';
        $paginate = 15;
        $list     = Db::name('counselor_order')->alias('a')->join($join)->where($where)->field($field)->order('a.co_addtime desc')->paginate($paginate);
        $page     = $list->render();
        $data['list'] = $list;
        $data['page'] = $page;
        $data['post'] = $param?$param:null;
        return $data;
    }

    //服务详情
    function seveFind(){
        $counselor_order_id = input('counselor_order_id');
        $join = [
            ['lt_user b','a.u_uid=b.u_uid'],
        ];
        $where['a.counselor_order_id'] = $counselor_order_id;
        $field        = 'a.*,b.u_phone,b.u_nickname';
        $data         = Db::name('counselor_order')->alias('a')->join($join)->where($where)->field($field)->find();
        $data['list'] = explode('&',$data['co_ps']);
        return    $data;
    }


    //服务操作
    function serveHandleApi($counselor_order_id,$state,$co_age_limit){
        if(empty($counselor_order_id) || empty($state) || empty($co_age_limit)){
            return messageInfo('101','系统参数错误!');
        }
        $date = date('Y-m-d H:i:s');
        if($state == 1){
            $data_aray['co_begintime']  = $date;
            $data_aray['co_finishtime'] = date("Y-m-d H:i:s",strtotime("+$co_age_limit year"));
            $data_aray['co_state']      = 2;
        }else{
            $data_aray['co_state']      = 4;
        }
        $data_aray['co_handletime']  = $date;
        $a     = Db::name('counselor_order')->where('counselor_order_id',$counselor_order_id)->update($data_aray);
        if(!$a){
            return messageInfo('101','操作失败!');
        }
        return messageInfo('202','操作成功!');
    }

    //服务查询顾问卡
    function counselorList(){
        $list = Db::name('counselor')->field('counselor_id,ck_name,ck_price')->order('ck_sort desc,ck_addtime desc')->select();
        return $list;
    }

    //新增顾问卡
    function serveAnyRequestApi($input){
        if(empty($input['co_age_limit']) || empty($input['u_phone'])){
            return messageInfo('101','服务年限与用户手机号码必需填写!');
        }
        $va_phone = preg_match('/^1\d{10}$/', $input['u_phone']);
        if (!$va_phone) {
            return messageInfo('101', '用户手机号码格式错误');
        }
        if(!is_numeric($input['co_age_limit']) || $input['co_age_limit'] >= 5){
            return messageInfo('101','服务年限必需为数字或必需小于5年!');
        }
        $data = Db::name('counselor')->where('counselor_id',$input['counselor_id'])->field('ck_name,ck_price,ck_ps')->find();
        if(empty($data)){
            return messageInfo('101', '服务类型不存在!');
        }
        $u_uid = Db::name('user')->where('u_phone',$input['u_phone'])->value('u_uid');
        if(empty($u_uid)){
            return messageInfo('101', '用户不存在!');
        }
        $co_no   = 'gw_'.date('YmdHis');
        $date = date('Y-m-d H:i:s');
        $co_age_limit = $input['co_age_limit'];
        $data_array = array(
            'u_uid'          => $u_uid,
            'co_no'          => $co_no,
            'co_name'        => $data['ck_name'],
            'co_money'       => $data['ck_price'] * $co_age_limit,
            'co_age_limit'   => $co_age_limit,
            'co_content'     => $input['co_content'],
            'co_ps'          => $data['ck_ps'],
            'co_state'       => 2,
            'co_addtime'      =>$date,
            'co_begintime'   => $date,
            'co_finishtime'  => date("Y-m-d H:i:s",strtotime("+$co_age_limit year"))
        );
        $a = Db::name('counselor_order')->insert($data_array);
        if(!$a){
            return messageInfo('101', '提交失败!');
        }
        $url = config('tempredirect_url').'order/serve_list/';
        return messageInfo('202', '提交成功!','',$url);
    }




}
